package com.levi.completableFuture;

import java.util.concurrent.*;

/**
 * @author shentong
 * @since 2022/1/16 7:27 PM
 * acceptEither方法当前面的异步任务有一个执行完就开始执行，并且会获取前面任务的执行结果
 * acceptEither方法没有返回值
 */
public class AcceptEitherDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }, executorService);
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 2;
        }, executorService);
        CompletableFuture<Void> future = future1.acceptEitherAsync(future2, value -> {
            System.out.println("只要有一个异步任务执行完我就执行>>" + value);
        },executorService);
//        try {
//            System.out.println(future.get());
//        } catch (InterruptedException | ExecutionException e) {
//            e.printStackTrace();
//        }
        System.out.println("Main end~~~");
        executorService.shutdown();
    }
}
